package com.foreknow.elm.controller;

import com.foreknow.elm.po.Cart;
import com.foreknow.elm.service.impl.CartServiceImpl;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

public class CarController {
    /**
     * 根据用户编号查询此用户所有购物车信息
     * 根据用户编号和商家编号，查询此用户购物车中某个商家的所有购物车信息
     * @param request
     * @return
     * @throws Exception
     */
    public Object listCart(HttpServletRequest request)throws Exception{
        List<Cart> list = new ArrayList<>();
        String userId = request.getParameter("userId");
        CartServiceImpl cartService = new CartServiceImpl();
        try {
            Integer businessId = Integer.valueOf(request.getParameter("businessId"));
            list = cartService.listCart(userId,businessId);
        } catch (Exception e) {
            list = cartService.listCart(userId);
        }
        return list;
    }

    /**
     * 向购物车表中添加一条记录
     * @param
     * @return
     */
    public int saveCart(HttpServletRequest request)throws Exception {
        int result = 0;
        Integer foodId = Integer.valueOf(request.getParameter("foodId"));
        Integer businessId = Integer.valueOf(request.getParameter("businessId"));
        String userId = request.getParameter("userId");
        Integer quantity = Integer.valueOf(request.getParameter("quantity"));
        Cart cart = new Cart(foodId,businessId,userId,quantity);
        CartServiceImpl cartService = new CartServiceImpl();
        try {
            result = cartService.saveCart(cart);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 根据用户编号、商家编号、食品编号更新数量
     * @param request
     * @return
     */
    public int updateCart(HttpServletRequest request)throws Exception{
        int result = 0;
        Integer foodId = Integer.valueOf(request.getParameter("foodId"));
        Integer businessId = Integer.valueOf(request.getParameter("businessId"));
        String userId = request.getParameter("userId");
        Integer quantity = Integer.valueOf(request.getParameter("quantity"));
        Cart cart = new Cart(foodId,businessId,userId,quantity);
        CartServiceImpl cartService = new CartServiceImpl();
        try {
            result = cartService.updateCart(cart);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     *  根据用户编号、商家编号删除购物车表中的多条条记录
     * @param request
     * @return
     */
    public int removeCart(HttpServletRequest request)throws Exception{
        int result = 0;
        Integer businessId = Integer.valueOf(request.getParameter("businessId"));
        String userId = request.getParameter("userId");
        CartServiceImpl cartService = new CartServiceImpl();
        try {
            Integer foodId = Integer.valueOf(request.getParameter("foodId"));
            result = cartService.removeCart(userId,businessId,foodId);
        } catch (Exception e) {
            result = cartService.removeCart(userId, businessId);
        }
        return result;
    }
    }

